<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use EasyWeChat\Factory;
use EasyWeChat\Foundation\Application;
use function EasyWeChat\Kernel\Support\generate_sign;

class UserController extends Controller
{

// https://beize.sdjfe.com/api/test登录测试
  public function test()
  {
    return "api登录测试+++";
  }

  public function login(Request $request)
  {
        // $code = $request->input('code');
        // return response()->json([
        //       'code' => 200,
        //       'msg'=>'验证成功',
        //   ], 200);
        // $appid = 'wxaacc4d201a5f3f45';
        // $appsecret = 'b451792bd3cd6b30691589b4d5d630a6';
        $name = $request->input('name');
        $avatar = $request->input('avatar');
        $openid = $request->input('openid');
        $user = User::where('openid', $openid)->get();
        $user_id = $user[0]['id'];
        // $this->validate(request(),[
        //   'openid'='required:2'
        // ]);
        //逻辑
        Auth::loginUsingId($user_id);
        $openid = $request->input('openid');
        $user1 = Auth::user();
         session(['user1' => $user1]);
          $value = session('user1');
          // return $value."dyp";
        // $id = Auth::id();
        // return $user[0]['id'];
       //  if (Auth::attempt(['openid' => $openid])) {
       //     // 认证通过...
       //     return 'ok';
       // }
        // return $user1;
        // if(Auth::guard("api")->attempt($openid)){
        //  return "登录ok";
        // }
        $userCount = count($user);
        if($userCount == 1){
          return response()->json([
                'code' => 200,
                'msg'=>'验证登录成功',
                'name' =>$name,
                'user' =>$user1
            ], 200);
        }else {
        // return $request->input('openid');
        $user = new User;
        // $user->name = $request->input('name');
        // return $request->input('name');
        $user->openid = $request->input('openid');
        $user->avatar = $request->input('avatar');
        $user->save();
        }

  }
    //获取用户信息，类似登录功能
    public function getUser(Request $request)
    {
      $params  = $request->all();
      // return $params;
        // $code = $request->input('jsCode');
        // $miniProgram = \EasyWeChat::miniProgram();
        // $app = $miniProgram->auth->session($code);
        // $openid = $app['openid'];
      $openid = $request->input('openid');
      $name = $request->input('name');
      $avatar = $request->input('avatar');
      // $code = $request->input('code');
        $user = User::where('openid', $openid)->get();
        $userCount = count($user);
        if($userCount == 1){
          return response()->json([
                'code' => 200,
                'msg'=>'验证登录成功'
            ], 200);
    }else {
       $user = new User();
       $user->openid = $openid;
       // $user->name = $name;
       // $user->avatar = $avatar;
       $user->save();
       return $name;
    }

      // return  getenv('WECHAT_MINI_PROGRAM_APPID') .'+'.getenv('WECHAT_MINI_PROGRAM_SECRET');
    }

    public function register(Request $request)
    {
      $openid = $request->input("openid");
      // $name = $request->input("nickname");
      $avatar = $request->input("head");
      $user = new User;

       $user->openid = $openid;
       $user->name = $name;
       $user->avatar = $avatar;
       $user->save();
    }
      //校验openid
   // public function checkOpenid(Request $request){
   //     $appid = getenv('WECHAT_MINI_PROGRAM_APPID');
   //     $secret = getenv('WECHAT_MINI_PROGRAM_SECRET');
   //     $js_code = $request->input("code");
   //     $openid = $request->input("openid");//用户请求过来的值
   //
   //     $url = 'https://api.weixin.qq.com/sns/jscode2session';
   //     $data = array(
   //         'appid' => $appid,
   //         'secret' => $secret,
   //         'js_code' => $js_code,
   //         'grant_type' => 'authorization_code',
   //     );
   //
   //     $res = httpRequest($url, 'POST', $data);
   //     //输出测试，正式使用请删除下面一行
   //     //输出{"session_key":"GxT18piX7JEvUhazrrcsxw==","openid":"oEE2t4n0eerWnb2mNShyK2ttXLc0"}
   //     file_put_contents("../log.txt", $res, FILE_APPEND);
   //
   //     $obj = json_decode($res); //返回数组或对象
   //     if($obj->openid == $openid){
   //         return true;
   //     }else{
   //         return false;
   //     }
   //  }
   //
    public function getOpenid(Request $request)
    {
      $code = $request->input('code');
      $miniProgram = \EasyWeChat::miniProgram();
      $app = $miniProgram->auth->session('01175CeL128VJ41x7BeL1FkieL175Cef');
      return $app['openid'];
      $user = $app->user->get($app['openid']);
      return $user;
    }
}
